//
//  HWGroupChatRecommendController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/25.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGroupChatRecommendController: HWBaseViewController {
    var bgView:UIView!
    var topImageView:UIImageView!
    var tableView:UITableView!
    var tableViewProtocol = HWGroupChatRecommendProtocol()
    var httpManager = HWGroupChatRecommendHttpManager()
    var models:[HWGroupChatRecommendModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        addRefresh()
        
        loadData()
        
        addAction()
        
    }

}

extension HWGroupChatRecommendController {
    private func initUI() -> Void {
        
        navigationView.titleLabel.text = ""
        navigationView.backButton.setImage(UIImage(named: "navBackButton"), for: .normal)
        view.addSubview(navigationView)
        
        bgView = UIView()
        bgView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
        view.addSubview(bgView)
        UIColor.gradientColor(view: bgView, colors: [UIColor.colorWithHexString(hexString: "#3CEDB5").cgColor,
                                                     UIColor.colorWithHexString(hexString: "#1CD67E").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: 1))
        
        
        topImageView = UIImageView()
        topImageView.image = UIImage(named: "groupChatRecommendTop")
        bgView.addSubview(topImageView)
        
        view.bringSubviewToFront(navigationView)
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .colorWithHexString(hexString: "#FFFFFF")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
        if #available(iOS 15.0, *) {
            self.tableView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }
        
        tableView.layer.cornerRadius = 18*widthScale
        tableView.layer.masksToBounds = true
    }
    
    private func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        topImageView.snp.makeConstraints { make in
            make.top.equalTo(51*widthScale)
            make.left.right.equalToSuperview()
            make.height.equalTo(225*widthScale)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(260*widthScale)
            make.centerX.equalToSuperview()
            make.width.equalTo(339*widthScale)
            make.height.equalTo(498*widthScale)
        }
        
        
    }
}

extension HWGroupChatRecommendController {
    func loadData() -> Void {
//        tableView.mj_footer?.isHidden = true
        httpManager.getGroupChatRecommend { models in
            self.models.removeAll()
            self.tableView.mj_header?.endRefreshing()
//            self.tableView.mj_footer?.endRefreshing()
            for m in models {
                self.models.append(m)
            }
            self.tableViewProtocol.models = self.models
            self.tableView.reloadData()
        } failure: { errorString in
            self.models.removeAll()
            self.tableViewProtocol.models = self.models
            self.tableView.reloadData()
//            self.tableView.mj_footer?.isHidden = true
            self.tableView.mj_header?.endRefreshing()
//            self.tableView.mj_footer?.endRefreshing()
        }

    }
    
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
//        tableView.mj_footer = MJRefreshBackNormalFooter()
//        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
//        tableView.mj_footer?.isHidden = true
    }
//
//
    @objc func loadNewData() -> () {
        loadData()
    }
//
//    @objc func loadMoreData() -> () {
//        self.tableView.mj_footer?.endRefreshing()
//        httpManager.getGameCommunityAllListFooter(type: type, keyword: keyword) { models in
//            self.tableView.mj_footer?.endRefreshing()
//            for m in models {
//                self.models.append(m)
//            }
//            self.tableViewProtocol.models = self.models
//            self.tableView.reloadData()
//        } failure: { errorString in
//            self.tableView.mj_footer?.endRefreshing()
//        }
//
//    }
}

extension HWGroupChatRecommendController {
    func addAction() -> Void {
        self.tableViewProtocol.joinButtonClick = {
            mod in
            if mod.url.count > 0 {
                if HWLocallyStoredData.getLoginUserID().count > 0 {
                    let webVC = XMGWebViewController()
                    webVC.url = URL(string: mod.url)
                    webVC.isGroup = true
                    self.navigationController?.pushViewController(webVC, animated: true)
                } else {
                    let loginController = HWLoginController()
                    self.navigationController?.pushViewController(loginController, animated: true)
                }
            } else {
                self.view.showHUD(title: "敬请期待")
            }
        }
    }
}
